package soulkun.library.custom.view

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.ViewGroup
import android.widget.Scroller
import soulkun.library.utils.logE
import soulkun.library.utils.toPxF
import kotlin.math.absoluteValue
import kotlin.math.max

/**
 * 仿写微信左滑出现删除的viewGroup
 */
class SwipeLayout : ViewGroup {

    companion object{
        const val TAG = "SwipeLayout"
    }

    private var downX = 0f
    private var moveX = 0f
    private var moved = 0f
    private var scroller: Scroller = Scroller(context)
    private var isShow = false

    constructor(context: Context) : this(context, null)

    constructor(context: Context, attributeSet: AttributeSet?) : this(context, attributeSet, 0)

    constructor(context: Context, attributeSet: AttributeSet?, defStyleAttr: Int): super(context, attributeSet, defStyleAttr)

    /**
     * 测量，宽度为全部宽度总和，高度为第一个子项高度
     */
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        measureChildren(widthMeasureSpec, heightMeasureSpec)
        val width = MeasureSpec.getSize(widthMeasureSpec) //测量全部子项的宽度
        var height = 0
        for (index in 0 until childCount) {
            val childHeight = getChildAt(index).measuredHeight //高度为第一个子项的高度
            height = max(height, childHeight)
        }
        setMeasuredDimension(width, height)
    }

    /**
     * 布局，第一个子项固定布满宽度，其他子项则是根据自身宽度布局
     */
    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        for (index in 0 until childCount) {
            val child = getChildAt(index)
            if (index == 0) {
                child.layout(l, (t + b) / 2 - child.measuredHeight / 2, r, (t + b) / 2 + child.measuredHeight / 2)
            }else{
                child.layout(r, (t + b) / 2 - child.measuredHeight / 2, r + child.measuredWidth, (t + b) / 2 + child.measuredHeight / 2)
            }
        }
    }

    override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
        when (ev?.action) {
            MotionEvent.ACTION_DOWN -> {
                downX = ev.rawX
            }
            MotionEvent.ACTION_MOVE -> {
                moveX = ev.rawX
                if ((downX - moveX).absoluteValue > 10){
                    parent.requestDisallowInterceptTouchEvent(true)
                    return true
                }
            }
            MotionEvent.ACTION_UP -> {
            }
        }
        return super.onInterceptTouchEvent(ev)
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        when (event?.action) {
            MotionEvent.ACTION_DOWN -> {
                downX = event.rawX
            }
            MotionEvent.ACTION_MOVE -> {
                moveX = event.rawX
                moved = downX - moveX
                if (isShow) {
                    moved += getChildAt(1).measuredWidth
                }
                scrollTo(moved.toInt(), 0)
                //超出界限时
                if (moved <= 0) {
                    scrollTo(0, 0)
                } else if (moved >= getChildAt(1).measuredWidth) {
                    scrollTo(getChildAt(1).measuredWidth, 0)
                }
            }
            MotionEvent.ACTION_CANCEL, MotionEvent.ACTION_UP -> {
                if (scrollX >= getChildAt(1).measuredWidth / 2) {
                    isShow = true
                    smoothScrollToPosition(getChildAt(1).measuredWidth)
                } else {
                    isShow = false
                    smoothScrollToPosition(0)
                }
            }
        }
        return true
    }

    private fun smoothScrollToPosition(destX: Int) {
        val deltaX = destX - scrollX
        scroller.startScroll(scrollX, 0, deltaX, 0, 100)
        invalidate()
    }

    override fun computeScroll() {
        super.computeScroll()
        if (scroller.computeScrollOffset()) {
            scrollTo(scroller.currX, scroller.currY) // 调用Scroller中存储的值
            invalidate()
        }
    }

}